package com.neutech.base;

import com.neutech.Util.ImageUtils;
import com.neutech.constant.Constant;
import com.neutech.frame.TankFrame;
import com.neutech.terrain.*;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 地图生成器
 */
public class MapGenerator implements Drawable {

    private List<Image> imageList = new ArrayList<>();
    private TankFrame tankFrame;
//跳转到初始界面传过来tankmap
    public MapGenerator(TankFrame tankFrame) {
        loadingImg();
        this.tankFrame = tankFrame;
    }

    private double index;

    private int x = Constant.MARGIN_LEFT;
    private int y = Constant.MARGIN_TOP;

    public TankMap tankMap = new TankMap();//空的坦克地图//TODO 多余要传的

    @Override
    public void draw(Graphics g) {
        /**
         * 设置画笔颜色rgb的值，这是大背景色,这个得卸载背景大小的前面
         */
        g.setColor(new Color(127, 127, 127));
        /**
         * 填充背景色
         */
        g.fillRect(0, 0, Constant.FRAME_WIDTH, Constant.FRAME_HEIGHT);

        /**
         * 设置画笔黑色
         */
        g.setColor(Color.BLACK);
        g.fillRect(Constant.MARGIN_LEFT, Constant.MARGIN_TOP, Constant.BLACK_SIZE, Constant.BLACK_SIZE);
        tankMap.gameRunning(g);
        /**
         * 绘制一个闪烁的坦克
         */
        index += 0.05;
        if (index > 2) {
            index = 0;
        }
        g.drawImage(imageList.get((int) Math.floor(index)), x, y, null);

    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            y -= Constant.SPRITE_SIZE;
        }
        if (e.getKeyCode() == KeyEvent.VK_D) {
            x += Constant.SPRITE_SIZE;
        }
        if (e.getKeyCode() == KeyEvent.VK_S) {
            y += Constant.SPRITE_SIZE;
        }
        if (e.getKeyCode() == KeyEvent.VK_A) {
            x -= Constant.SPRITE_SIZE;
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            if (isExist()) {
                /**
                 * isExist()判断是否有元素，有就向下生成
                 */
                generateIndex++;
                generateSprite();
            }
            /**
             * 没有的话就在当前位置生成
             */
            else {
                generateSprite();
            }


        }
        if (e.getKeyCode() == KeyEvent.VK_R) {
            //R进行保存成文件
            generateMapFile();
            //生成地图文件回到初始状态，调到开始界面
            tankFrame.changeStatus(1);

        }
        //TODO 需要边缘检测
        outOfBounds();

    }
    public void outOfBounds(){
        if (x > (Constant.MARGIN_LEFT + Constant.SPRITE_SIZE*12)){
            x = Constant.MARGIN_LEFT + Constant.SPRITE_SIZE*12;
        }else if (x < Constant.MARGIN_LEFT){
            x = Constant.MARGIN_LEFT;
        }else if (y > (Constant.MARGIN_TOP + Constant.SPRITE_SIZE*12)){
            y = Constant.MARGIN_TOP + Constant.SPRITE_SIZE*12;
        }else if (y < Constant.MARGIN_TOP){
            y = Constant.MARGIN_TOP;
        }
    }



    /**
     * 生成地图文件
     */
    public void generateMapFile() {
        OutputStream os = null;
        ObjectOutputStream oos = null;
        try {
            os = new FileOutputStream("src/com/neutech/map/stage1.tankmap");
            oos = new ObjectOutputStream(os);
            oos.writeObject(tankMap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private int generateIndex = 1;

    /**
     * 判断当前位置是否有元素
     *
     * @return
     */
    public boolean isExist() {
        //判断与地图中所有的墙草水冰是否相交
        int wallCount = 0;//记录多个墙相交的个数
        for (Wall wall : tankMap.wallList) {
            if (wall.getRectangle().intersects(getRectangle())) {
                tankMap.wallList.remove(wall);//因为5墙是两个，一单检测到就清楚就停止了，第二个墙可能就没有检测到
                wallCount++;
//                return true;//没机会判断了就
            }
        }
        if (wallCount > 0) {
            return true;
        }
        for (Water water : tankMap.waterList) {
            if (water.getRectangle().intersects(getRectangle())) {
                //清除水
                tankMap.waterList.remove(water);
                return true;
            }
        }
        for (Grass grass : tankMap.grassList) {
            if (grass.getRectangle().intersects(getRectangle())) {
                tankMap.grassList.remove(grass);
                return true;
            }
        }
        for (Ice ice : tankMap.iceList) {
            if (ice.getRectangle().intersects(getRectangle())) {
                tankMap.iceList.remove(ice);
                return true;
            }
        }
        return false;
    }

    public void generateSprite() {
        /**
         * 判断6个特殊点位，不让他生成
         */
        if ((x == Constant.MARGIN_LEFT && y == Constant.MARGIN_TOP)
                || (x == Constant.MARGIN_LEFT + Constant.SPRITE_SIZE * 6 && y == Constant.MARGIN_TOP)
                || (x == Constant.MARGIN_LEFT + Constant.SPRITE_SIZE * 12 && y == Constant.MARGIN_TOP)
                || (x == Constant.MARGIN_LEFT + Constant.SPRITE_SIZE * 4 && y == Constant.MARGIN_TOP + Constant.SPRITE_SIZE * 12)
                || (x == Constant.MARGIN_LEFT + Constant.SPRITE_SIZE * 8 && y == Constant.MARGIN_TOP + Constant.SPRITE_SIZE * 12)
        ) {
            return;

        }
        //右
        if (generateIndex == 1) {
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 1));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 1));
        }
        //下
        if (generateIndex == 2) {
            tankMap.wallList.add(new Wall(x, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 1));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 1));
        }
        //左
        if (generateIndex == 3) {
            tankMap.wallList.add(new Wall(x, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 1));
            tankMap.wallList.add(new Wall(x, y,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 1));
        }
        //上
        if (generateIndex == 4) {
            tankMap.wallList.add(new Wall(x, y,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 1));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 1));
        }//全
        if (generateIndex == 5) {
            tankMap.wallList.add(new Wall(x, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 1));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 1));
            tankMap.wallList.add(new Wall(x, y,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 1));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 1));
        }//右
        if (generateIndex == 6) {
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 2));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 2));
        }
        //下
        if (generateIndex == 7) {
            tankMap.wallList.add(new Wall(x, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 2));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 2));
        }
        //左
        if (generateIndex == 8) {
            tankMap.wallList.add(new Wall(x, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 2));
            tankMap.wallList.add(new Wall(x, y,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 2));
        }
        //上
        if (generateIndex == 9) {
            tankMap.wallList.add(new Wall(x, y,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 2));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 2));
        }
        //全
        if (generateIndex == 10) {
            tankMap.wallList.add(new Wall(x, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 2));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y + Constant.WALL_SIZE,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 2));
            tankMap.wallList.add(new Wall(x, y,
                    Constant.WALL_SIZE, Constant.WALL_SIZE, 2));
            tankMap.wallList.add(new Wall(x + Constant.WALL_SIZE, y,
                    Constant.WALL_SIZE,
                    Constant.WALL_SIZE, 2));
        }
        if (generateIndex == 11) {
            tankMap.waterList.add(new Water(x, y, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));

        }
        if (generateIndex == 12) {
            tankMap.grassList.add(new Grass(x, y, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));

        }
        if (generateIndex == 13) {
            tankMap.iceList.add(new Ice(x, y, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE));
        }
        if (generateIndex == 14) {
            isExist();//移除内容  遇到问题未移除出bug原因在与没有写ice的Rectangle
            generateIndex = 1;
        }

    }


    public void loadingImg() {
        imageList.add(ImageUtils.getValue("tank_y_1_1"));
        imageList.add(ImageUtils.getValue("space"));
    }

    public Rectangle getRectangle() {
        return new Rectangle(x, y, Constant.SPRITE_SIZE, Constant.SPRITE_SIZE);
    }

}
